<?php
class ViewHistoryService extends BaseDbService
{
	private $searchService;
	
	public function __construct()
	{
		$this->dao				= new ViewHistoryDao();
	}
	
	public function getCountByDate($d,$type)
	{
		$condition['where']['expression']		= 'type=:type and updated <="'.$d.' 23:59:59" and updated>="'.$d.' 00:00:00" ';
		$condition['where']['value']['type']		= $type;
		
		return $this->count($condition);
	}
	
	public function updateViewHistory($numId,$type,$num=1,$nick='')
	{
		if($numId==0)
		{
			return 0;
		}
		
		if($nick!='' && $type==2)
		{
			$ccSetting = C('SettingService')->get('click_count_setting',true);
			if(array_key_exists('nick',$ccSetting))
			{
				$nicks = explode(',',$ccSetting['nick']);
				if(in_array($nick,$nicks))
				{
					$params			= array(
						'method' 	=> 'taobao.taobaoke.items.view.count',
						'id'		=> $numId,
						'type'		=> $type,
						'pisces'	=> true
					);
					C('SearchService')->getResult($params);
				}
			}
		}
		
		$condition['expression']		= 'num_id=:num_id and type=:type';
		$condition['value']['num_id']	= $numId;
		$condition['value']['type']		= $type;
		$tmp	= $this->fetch($condition);
		if(!$tmp)
		{	//���¹����¼modify by tt596 20111023 �����θ�����ɹ��������ۼ�
			//return $this->add(array('num_id'=>$numId,'type'=>$type,'view_num'=>1));
			return $this->add(array('num_id'=>$numId,'type'=>$type,'view_num'=>$num));
			//���¹����¼modify by tt596 20111023 �����θ�����ɹ��������ۼ�
		}
		else
		{
			return $this->dao->addViewNum($tmp['id'], $num);
		}
	}
	
	public function delByPeriod($type,$start,$end)
	{
		$du		= C('DateUtil');
		if(!$du->isDate($start))
		{
			$start	= '';
		}
		if(!$du->isDate($end))
		{
			$end	= '';
		}
		
		$condition['expression']		= 'type=:type';
		if($start!='')
		{
			$condition['expression']	.= ' and created>="'.$start.' 00:00:00"';
		}
		if($end!='')
		{
			$condition['expression']	.= ' and created<="'.$end.' 23:59:59"';
		}
		$condition['value']['type']		= $type;
		
		return $this->deleteRows($condition);
	}
	
	public function getRecentViewHistory($num,$fields='',$cache=0,$position='')
	{
		return $this->getListFromCache(array('type'=>1),'updated DESC',0,$num,$fields,$cache,'view_history_recent'.$position);
	}
	
	public function getRecentClickHistory($num,$fields='',$cache=0,$position='')
	{
		return $this->getListFromCache(array('type'=>2),'updated DESC',0,$num,$fields,$cache,'click_history_recent'.$position);
	}
	
	public function getRecentBuyHistory($num,$fields='',$cache=0,$position='')
	{
		return $this->getListFromCache(array('type'=>3),'updated DESC',0,$num,$fields,$cache,'buy_history_recent'.$position);
	}
	
	public function getHotViewHistory($num,$fields='',$cache=0,$position='')
	{
		return $this->getListFromCache(array('type'=>1),'view_num DESC',0,$num,$fields,$cache,'view_history_hot'.$position);
	}
	
	public function getHotClickHistory($num,$fields='',$cache=0,$position='')
	{
		return $this->getListFromCache(array('type'=>2),'view_num DESC',0,$num,$fields,$cache,'click_history_hot'.$position);
	}
	
	public function getHotBuyHistory($num,$fields='',$cache=0,$position='')
	{
		return $this->getListFromCache(array('type'=>3),'view_num DESC',0,$num,$fields,$cache,'buy_history_hot'.$position);
	}
}
?>